<?php

namespace App\Supports\Payment\Methods\Adawxmini;

use Illuminate\Support\Facades\Log;
use App\Supports\Payment\Methods\Method;
use App\Supports\Payment\Contracts\MethodCallback;

class CallbackAdapay extends Method implements MethodCallback
{
	public function markAsPaid($order = null, $data = null) {}

	public function validate($data = null) {}

	protected function paidCallback($data)
	{
		$tag = $this->getPaidTag($data['order_no']);

		if ($tag == 'order') {
			return cashier('adawxmini')->callbackOrderPay($data);
		} elseif ($tag == 'vip') {
			return cashier('adawxmini')->callbackVipPay($data);
		} elseif ($tag == 'charge') {
            return cashier('adawxmini')->callbackGoldChargePay($data);
        }

		return 'fail';
	}

	protected function refundCallback($data)
	{
		$tag = $this->getPaidTag($data['payment_order_no']);

		if ($tag == 'order') {
			return cashier('adawxmini')->callbackOrderRefund($data);
		} elseif ($tag == 'vip') {
			return cashier('adawxmini')->callbackVipRefund($data);
		}
		return 'fail';
	}

	public function callback($data = null)
	{
		$data = request('data');
		$sign = request('sign');

		if (!$data or !$sign) return 'fail';

		$payTools = $this->channel->tools();

		$verified = $payTools->verifySign($data, $sign);

		$data = json_decode($data, true);

		if ($verified and ($data['status'] == 'succeeded')) {

			if (isset($data['object'])
				and isset($data['refund_order_no'])) {
				return $this->refundCallback($data);
			}
			return $this->paidCallback($data);
		} else {
			return 'fail';
		}
	}
}
